package de.baumann.browser.activitys.hash

import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import com.amap.api.location.AMapLocation
import com.amap.api.maps.AMap
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.UiSettings
import com.amap.api.maps.model.*
import de.baumann.browser.R
import de.baumann.browser.activitys.hash.SendPackActivity.Companion.startSendPackActivity
import de.baumann.browser.api.net.vo.PacketMark
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.iview.IGetPacksView
import de.baumann.browser.present.GetPacksPresenter
import de.baumann.browser.units.LocationUnit
import de.baumann.browser.utils.isLogined
import de.baumann.browser.views.dialog.Callback
import de.baumann.browser.views.dialog.OpenRedPacketDialog
import de.baumann.browser.views.dialog.VaptchaDialog
import kotlinx.android.synthetic.main.activity_red_packet.*
import kotlinx.android.synthetic.main.include_tool_bar.*
import java.util.*
import kotlin.collections.ArrayList
import kotlin.math.cos
import kotlin.math.sin
import kotlin.math.sqrt

/**
 *@author:
 *@date:2019/7/9 17:49
 *@discription: 抢红包
 */
class RedPacketActivity : BaseOdinActivity<IGetPacksView, GetPacksPresenter>(), IGetPacksView {

    private lateinit var aMap: AMap
    private lateinit var myLocationStyle: MyLocationStyle
    private lateinit var uiSetting: UiSettings
    private var aMapLocation: AMapLocation? = null
    private var adUrl = ""

    companion object {
        fun startRedPacketActivity(context: Context) {
            context.startActivity(Intent(context, RedPacketActivity::class.java))
        }
    }

    override fun getLayout(): Int {
        return R.layout.activity_red_packet
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView(savedInstanceState)
    }

    override fun initView() {
        initToolbar(commToolBar, commTitle)
        ivSendPacket.setOnClickListener {
            if (isLogined()) {
                startSendPackActivity(mContext)
            } else {
            }
        }
    }

    fun initView(savedInstanceState: Bundle?) {
        mapView.onCreate(savedInstanceState)
        aMap = mapView.map
        //地图的缩放级别一共分为 17 级，从 3 到 19。数字越大，展示的图面信息越精细。
        aMap.moveCamera(CameraUpdateFactory.zoomTo(17.8f))
        myLocationStyle = MyLocationStyle()
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE)
        myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(resources, R.drawable.ic_location)))

        //设置定位蓝点精度圆圈的填充颜色的方法。
        myLocationStyle.radiusFillColor(Color.argb(0, 0, 0, 0))

        //        //设置定位蓝点精度圆圈的边框颜色的方法。
        myLocationStyle.strokeColor(Color.argb(0, 0, 0, 0))
        //        //设置定位蓝点精度圈的边框宽度的方法。
        //        myLocationStyle.strokeWidth(100f)
        aMap.myLocationStyle = myLocationStyle//设置定位蓝点的Style
        aMap.isMyLocationEnabled = true// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。


        uiSetting = aMap.uiSettings
        uiSetting.isZoomControlsEnabled = false
        uiSetting.isCompassEnabled = false


        aMap.showIndoorMap(true)

        aMap.setOnMarkerClickListener {
            val id = it.period
            if (id == 20) {
                return@setOnMarkerClickListener true
            }
//
//            if (!isLogined()) {
//                showLoginDialog()
//                return@setOnMarkerClickListener true
//            }
//
//            VaptchaDialog(mContext,object :Callback{
//                override fun callback(token: String) {
//                    runOnUiThread {
//                        presenter.getPackets1(token)
//                    }
//                }
//            }).show()

//            presenter.getPackets1()
            true
        }


        LocationUnit.initLocation(mContext.applicationContext) { aMapLocation ->
            if (aMapLocation != null && aMapLocation.errorCode == 0) {
                LocationUnit.stopLocation()
                this.aMapLocation = aMapLocation
                val latlng = LatLng(aMapLocation.latitude, aMapLocation.longitude)
                aMap.addCircle(CircleOptions().center(latlng)
                        .radius(200.0)
                        .fillColor(resources.getColor(R.color.blue_88))
                        .strokeColor(resources.getColor(R.color.blue_3A81F6))
                        .strokeWidth(3f))
                addRedPackets()
            }
        }
        LocationUnit.startLocation()
    }

    override fun createPresenter(): GetPacksPresenter? {
        return GetPacksPresenter()
    }

    override fun createView(): IGetPacksView? {
        return this
    }


    override fun initData() {
    }

    override fun getTitleText(): String {
        return getString(R.string.Looking_for_red_envelopes)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_redpacket, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        if (isLogined()) {
            PackRecordActivity.startPackRecordActivity(mContext)
        } else {
        }
        return super.onOptionsItemSelected(item)
    }


    private fun addRedPackets() {
        var latlng = LatLng(aMapLocation!!.latitude, aMapLocation!!.longitude)

        val arrMark = ArrayList<MarkerOptions>()

        for (i in 0..10) {
            latlng = getRandomLatLng(latlng)
            val markerOption = MarkerOptions()
            markerOption.position(latlng)
            markerOption.period(100 + i)
            markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(resources, R.drawable.ic_mark)))
            arrMark.add(markerOption)
        }
        aMap.addMarkers(arrMark, false)
    }


    private fun getRandomLatLng(point: LatLng): LatLng {
        val random = Random()
        val cirx = point.latitude
        val ciry = point.longitude
        val circleR = 0.000250

        val r = sqrt(Math.random()) * circleR
        val theta = random.nextInt(3600000) * 0.0001

        val x = r * sin(theta)
        val y = r * cos(theta)
        return LatLng(cirx + x, ciry + y)
    }

    override fun onResume() {
        super.onResume()
        mapView.onResume()

    }

    override fun onPause() {
        super.onPause()
        mapView.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        mapView.onDestroy()
    }


    override val local: String
        get() = aMapLocation!!.city

    override fun setPacket(pack: PacketMark) {
        if (!isDestroyed) {
            adUrl = pack.adUrl
            OpenRedPacketDialog(mContext)
                    .setPacket(pack)
                    .setOnOpenRedPacketListener(object : OpenRedPacketDialog.OnOpenRedPacketListener {
                        override fun onOpenRedPacket(packetId: Int) {
                            showCaptchaDialog(packetId)
                        }
                    })
                    .show()
        }
    }

    private fun showCaptchaDialog(packetId: Int) {
//        CAds.loadRewardVideoAd(this,"",object : CRewardVideoListener.CTRewardVideoListener {
//            override fun onRewardVerify(p0: Boolean, p1: Int, p2: String?) {
//            }
//
//            override fun onAdShow() {
//            }
//
//            override fun onVideoError() {
//            }
//
//            override fun onRewardVideoCached() {
//            }
//
//            override fun onSkippedVideo() {
//            }
//
//            override fun loadSuccess(p0: CRewardVideoAd?) {
//            }
//
//            override fun onAdVideoBarClick() {
//            }
//
//            override fun onVideoComplete() {
//                presenter.rePacket(packetId)
//            }
//
//            override fun onAdClose() {
//            }
//
//            override fun loadError(p0: Int, p1: String?) {
//            }
//        })
    }

    override fun openPacket(id: Int, shareUrl: String, amount: String) {
        RedPacketDetailActivity.startPacketDetailActivity(mContext, id, shareUrl, amount, adUrl)
    }
}
